package com.hfzy.ihk.web.aliyuncall.job;

import com.alibaba.fastjson.JSONObject;
import com.hfzy.ihk.common.util.FileUtils;
import com.hfzy.ihk.common.util.date.DateUtils;
import com.hfzy.ihk.web.aliyuncall.biz.PushDataToEsBiz;
import com.hfzy.ihk.web.aliyuncall.constant.RedisFields;
import com.hfzy.ihk.web.aliyuncall.constant.RedisKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.core.task.TaskExecutor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 阿里云晚上下载录音文件job
 */

@Component
public class CheckAndDownRecordFileJob {


    private static final Logger logger = LoggerFactory.getLogger(CheckAndDownRecordFileJob.class);
    @Value("${aliyun.save-record-file-path}")
    private String saveUrl;
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    PushDataToEsBiz pushDataToEsBiz;

    @Resource
    TaskExecutor pushDataToEsTaskExecutor;

    /**
     * 晚上十二点半半开始跑
     */
    @Scheduled(cron = "0 0/30 * * * ?")
    public void downRecordFile(){
        Set<String> urlSet = redisTemplate.opsForSet().members(RedisKey.ALIYUN_RECORD_URL_SET);
        for(String s:urlSet){
            try {
                String[] arg = s.split(":");
                if (arg != null && arg.length > 1) {
                    String sessionId = arg[0];
                    String recordFileUrl = arg[1];
                    String newReocrdFileUrl = saveUrl + DateUtils.formatDate(new Date(),"yyyyMMdd") +"/"+sessionId + ".wav";
                    //下载
                    try {
                        FileUtils.getRemoteFile(recordFileUrl, newReocrdFileUrl);
                        //下载成功删除redis key
                        redisTemplate.opsForSet().remove(RedisKey.ALIYUN_RECORD_URL_SET, s);
                        //推送
                        String thisUrl = newReocrdFileUrl.replace(saveUrl,"http://cloudcall.hfit.ihk.cn:8888/rec_20050/aliyuncall/");  //这里上线的时候要注意替换
                        Map<String,String> map = new HashMap<>();
                        map.put(RedisFields.CALL_HISTORY_SESSION_ID,sessionId);
                        map.put(RedisFields.CALL_HISTORY_RECORD_FILE_URL,thisUrl);
                        String jsonStr = JSONObject.toJSONString(map);
                        pushDataToEsTaskExecutor.execute(() -> {
                            pushDataToEsBiz.pushRecordFileToEs(sessionId, recordFileUrl,jsonStr);
                        });
                    } catch (Exception e) {
                        logger.error(">>>>>>>>>>>阿里云下载录音失败key:{}",s,e);
                    }

                }
            }catch (Exception e){
                logger.error(">>>>>>>>>>>跑下载录音Job异常,",e);
            }
        }

    }


}
